package com.lanou3g.zjl.chating.home.friends;

import android.graphics.Color;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.utils.ProgressLayout;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/21.
 */
public class CreateGroupChatActivity extends BaseActivity {
    @Bind(R.id.groupName)
    EditText groupName;//群名称
    @Bind(R.id.groupIntr)
    EditText groupIntr;//群简介
    @Bind(R.id.isPublic)
    AnimateCheckBox isPublic;//是否公开
    @Bind(R.id.isOpen)
    AnimateCheckBox isOpen;//开放群成员邀请
    @Bind(R.id.isNeedApprovalRequired)
    AnimateCheckBox isNeedApprovalRequired;//加群是否需要验证
    @Bind(R.id.save)
    TextView save;//保存
    @Bind(R.id.createGroupProgress)
    ProgressLayout createGroupProgress;//进度条
    @Override
    protected int getLayout() {
        return R.layout.create_group;
    }

    @Override
    protected void initData() {

        isPublic.setCircleColor(Color.GREEN);
        isOpen.setCircleColor(Color.GREEN);
        isNeedApprovalRequired.setCircleColor(Color.GREEN);
    }

    @OnClick(R.id.backToGroupList1)
    void backToGroupList1() {
        finish();
    }
    private void  cleanContent(){
        groupName.setText("");
        groupIntr.setText("");
        isPublic.setChecked(false);
        isOpen.setChecked(false);
        isNeedApprovalRequired.setChecked(false);
        createGroupProgress.showContent();
    }
    //点击保存按钮创建群
    @OnClick(R.id.save)
    void createNewGroup() {
        createGroupProgress.showProgress();
        if (!TextUtils.isEmpty(groupName.getText().toString())) {
            if (isPublic.isChecked()) {
                if (isNeedApprovalRequired.isChecked()) {
                    //创建公开群,需要群主验证
                    try {
                        //第四个参数:是否需要群主批准
                        EMGroupManager.getInstance().createPublicGroup(groupName.getText().toString(), groupIntr.getText().toString(), null, true);
                        Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
                        cleanContent();
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                } else {
                    //创建公开群,不需要验证
                    try {
                        //第四个参数:是否需要群主批准
                        EMGroupManager.getInstance().createPublicGroup(groupName.getText().toString(), groupIntr.getText().toString(), null, false);
                        Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
                        cleanContent();
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                }

            } else {
                //创建私有群:不能被搜索到，只能通过群主加人进群，或者设置了allowInvite为true，即允许群成员邀请
                //第一个参数：要创建的群聊的名称
                //第二个参数：群聊简介
                //第三个参数：群聊成员,为空时这个创建的群组只包含自己
                //第四个参数:是否允许群成员邀请人进群
                if (isOpen.isChecked()) {
                    try {
                        EMGroupManager.getInstance().createPrivateGroup(groupName.getText().toString(), groupIntr.getText().toString(), null, true);
                        Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
                        cleanContent();
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        EMGroupManager.getInstance().createPrivateGroup(groupName.getText().toString(), groupIntr.getText().toString(), null, false);
                        Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
                        cleanContent();
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                }

            }
        } else {
            Toast.makeText(CreateGroupChatActivity.this, "群名称不能为空", Toast.LENGTH_SHORT).show();
        }
    }
}
